package yong.birthdayreminder;

//import com.example.android.apis.R;
//import com.example.android.apis.view.List14.EfficientAdapter.ViewHolder;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class CurrentBithdayBuddyActivity extends ListActivity {
    /** Called when the activity is first created. */	
	//The cursor for all the current stars
    private BirthdayReminderDBHelper dbhelper = null;
    private SQLiteDatabase db = null;
    private String SQLSelectThisWeek = "select * from birthday where date(birthday) between date('now') and date('now', '+7 day');";
    private String SQLSelectThisMonth = "select * from birthday where date(birthday) between date('now') and date('now', '+30 day');";
    private String SQLSelect = null;
    private String scope = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		scope = (String)intent.getExtras().get("aweek");
		if (scope == null){
			SQLSelect = SQLSelectThisMonth;
		}else {
			SQLSelect = SQLSelectThisWeek;
		}
        dbhelper = new BirthdayReminderDBHelper(getBaseContext());
        db = dbhelper.getReadableDatabase();
        adaptOrUpdateList();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
        adaptOrUpdateList();
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Toast t = Toast.makeText(this, this.getString(R.string.prepareagift), 5);
		t.show();
	}
	private void adaptOrUpdateList (){
		Cursor bCursor = db.rawQuery(SQLSelect, null);
        startManagingCursor(bCursor);
		ListAdapter adapter = new SimpleCursorAdapter( 
                this, // Context. 
                R.layout.currentstar,  // Specify the row template to use (here, two columns bound to the two retrieved cursor rows). 
                bCursor,                                              // Pass in the cursor to bind to. 
                new String[] {"name", "hobby","birthday"},          // Array of cursor columns to bind to. 
                new int[] {R.id.text1, R.id.text2, R.id.text3});  // Parallel array of which template objects to bind to those columns. 
        setListAdapter(adapter);
	}
}
